package com.qiangshi.web.controller.realTimeInformation;

import com.qiangshi.common.annotation.Log;
import com.qiangshi.common.core.controller.BaseController;
import com.qiangshi.common.core.domain.AjaxResult;
import com.qiangshi.common.core.page.TableDataInfo;
import com.qiangshi.common.enums.BusinessType;
import com.qiangshi.common.utils.DateUtils;
import com.qiangshi.common.utils.PageUtils;
import com.qiangshi.common.utils.poi.ExcelUtil;
import com.qiangshi.realTimeInformation.domain.QsRealTimeInformation;
import com.qiangshi.realTimeInformation.domain.to.QsRealTimeInformationListTo;
import com.qiangshi.realTimeInformation.service.IQsRealTimeInformationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.*;

/**
 * RealTimeInformationController
 *
 * @author ccm
 * @date 2022-03-02
 */
@RestController
@RequestMapping("/realTimeInformation/realTimeInformation")
public class QsRealTimeInformationController extends BaseController
{
    @Autowired
    private IQsRealTimeInformationService qsRealTimeInformationService;

    /**
     * 查询RealTimeInformation列表
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:list')")
    @GetMapping("/list")
    public TableDataInfo list(QsRealTimeInformationListTo qsRealTimeInformation) {
        List<QsRealTimeInformation> list = qsRealTimeInformationService.selectQsRealTimeInformationList(qsRealTimeInformation);
        return PageUtils.buildTableDataInfo(qsRealTimeInformation.getPageNum(), qsRealTimeInformation.getSizeNum(), list);
    }

    /**
     * 查询RealTimeInformation列表
     */
    @GetMapping("/getBarChartsData")
    public AjaxResult getBarChartsData(QsRealTimeInformationListTo qsRealTimeInformation) {
        Map<String, Object> map = new HashMap<>();
        Calendar cal = Calendar.getInstance();
        if (Objects.isNull(qsRealTimeInformation.getStartTime2())) {
            cal.setTime(DateUtils.getNowDate());
            // 获得当前日期是一个星期的第几天
            int dayWeek = cal.get(Calendar.DAY_OF_WEEK);
            if (1 == dayWeek) {
                cal.add(Calendar.DAY_OF_MONTH, -1);
            }
            // 设置一个星期的第一天，按中国的习惯一个星期的第一天是星期一
            cal.setFirstDayOfWeek(Calendar.MONDAY);
            // 获得当前日期是一个星期的第几天
            int day = cal.get(Calendar.DAY_OF_WEEK);
            // 根据日历的规则，给当前日期减去星期几与一个星期第一天的差值
            cal.add(Calendar.DATE, cal.getFirstDayOfWeek() - day);
            cal.add(Calendar.WEEK_OF_YEAR, -1);
            qsRealTimeInformation.setStartTime(cal.getTime());
        }
        cal.setTime(qsRealTimeInformation.getStartTime2());
        cal.add(Calendar.DAY_OF_MONTH, 6);
        qsRealTimeInformation.setEndTime(cal.getTime());
        List<QsRealTimeInformation> list = qsRealTimeInformationService.selectQsRealTimeInformationList(qsRealTimeInformation);
        map.put("barChartsData", qsRealTimeInformationService.buildBarChartsVOList(qsRealTimeInformation, list));
        map.put("realTimeInformationListStatistics", qsRealTimeInformationService.selectQsRealTimeInformationListStatistics(qsRealTimeInformation, list));
        return AjaxResult.success(map);
    }

    /**
     * 导出RealTimeInformation列表
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:export')")
    @Log(title = "RealTimeInformation", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QsRealTimeInformationListTo qsRealTimeInformation) {
        List<QsRealTimeInformation> list = qsRealTimeInformationService.selectQsRealTimeInformationList(qsRealTimeInformation);
        ExcelUtil<QsRealTimeInformation> util = new ExcelUtil<QsRealTimeInformation>(QsRealTimeInformation.class);
        util.exportExcel(response, list, "RealTimeInformation数据");
    }

    /**
     * 获取RealTimeInformation详细信息
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(qsRealTimeInformationService.getById(id));
    }

    /**
     * 新增RealTimeInformation
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:add')")
    @Log(title = "RealTimeInformation", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody QsRealTimeInformation qsRealTimeInformation) {
        return toAjax(qsRealTimeInformationService.save(qsRealTimeInformation));
    }

    /**
     * 修改RealTimeInformation
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:edit')")
    @Log(title = "RealTimeInformation", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody QsRealTimeInformation qsRealTimeInformation) {
        return toAjax(qsRealTimeInformationService.updateById(qsRealTimeInformation));
    }

    /**
     * 删除RealTimeInformation
     */
    @PreAuthorize("@ss.hasPermi('realTimeInformation:realTimeInformation:remove')")
    @Log(title = "RealTimeInformation", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(qsRealTimeInformationService.removeByIds(Arrays.asList(ids)));
    }
}
